import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { DeviceMLongRange } from "@/api/backend/index";
import { ElNotification } from "element-plus"; //处理导航信息
import { getOverViewData } from "@/utils/common";

export const useLongRange = defineStore(Names.longRange, {
  state: () => {
    return {
      id: 0, //需要查询的设备id
      item: {
        addr: "",
        area: "",
        cTime: "",
        cardnumber: "",
        city: "",
        cname: "",
        code: "",
        coordinate: "",
        createBy: null,
        createTime: null,
        describ: null,
        estate: 1,
        gatewaycode: "",
        hearttime: 0,
        id: 0,
        institution: "",
        model: "",
        name: "",
        openState: 0,
        operating: null,
        params: {},
        plates: "",
        power: 0,
        process: "",
        producers: "",
        productName: "",
        production: null,
        province: "",
        recoveryDate: null,
        remark: null,
        replaceTime: null,
        searchValue: null,
        state: 0,
        street: "",
        updateBy: null,
        updateTime: null,
        user: "",
        userPhone: "",
        validity: null,
        volume: "0",
      }, //单个设备的告警列表
      opera: "add",
      order: "",
      fakeData: [
        {
          createBy: null,
          createTime: null,
          eId: 20,
          eName: "净化器",
          id: 2,
          params: {},
          remark: null,
          searchValue: null,
          setCode: "15",
          setName: "启动温度",
          setValue: "13",
          unit: "℃",
          updateBy: null,
          updateTime: null,
        },
        {
          createBy: null,
          createTime: null,
          eId: 20,
          eName: "净化器",
          id: 3,
          params: {},
          remark: null,
          searchValue: null,
          setCode: "16",
          setName: "停止温度",
          setValue: "40",
          unit: "℃",
          updateBy: null,
          updateTime: null,
        },
        {
          createBy: null,
          createTime: null,
          eId: 20,
          eName: "净化器",
          id: 4,
          params: {},
          remark: null,
          searchValue: null,
          setCode: "17",
          setName: "停止温度",
          setValue: "40",
          unit: "℃",
          updateBy: null,
          updateTime: null,
        },
        {
          createBy: null,
          createTime: null,
          eId: 20,
          eName: "净化器",
          id: 5,
          params: {},
          remark: null,
          searchValue: null,
          setCode: "18",
          setName: "压缩机启停",
          setValue: "启用",
          unit: "",
          updateBy: null,
          updateTime: null,
        },
        {
          createBy: null,
          createTime: null,
          eId: 20,
          eName: "净化器",
          id: 6,
          params: {},
          remark: null,
          searchValue: null,
          setCode: "19",
          setName: "压缩机开机压力",
          setValue: "1",
          unit: "bar",
          updateBy: null,
          updateTime: null,
        },
        {
          createBy: null,
          createTime: null,
          eId: 20,
          eName: "净化器",
          id: 7,
          params: {},
          remark: null,
          searchValue: null,
          setCode: "20",
          setName: "压缩机关机压力",
          setValue: "0.3",
          unit: "bar",
          updateBy: null,
          updateTime: null,
        },
      ],
      overviewData: {},
    };
  },
  getters: {},
  actions: {
    //获取远程控制参数列表 固定
    //删除远程控制配置
    deleteLR(data: anyObj) {
      return new Promise((resolve, reject) => {
        _delete(DeviceMLongRange + `/${data.id}`, {}, "json", "")
          .then((res: any) => {
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //新增远程运维配置
    addLR(data: anyObj) {
      return new Promise((resolve, reject) => {
        _post(DeviceMLongRange, data, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //编辑远程运维表单
    editLR(data: anyObj) {
      return new Promise((resolve, reject) => {
        _put(DeviceMLongRange, data, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //获取指令所需相应数据
    getOrderData(data: anyObj) {
      return new Promise((resolve, reject) => {
        _get(DeviceMLongRange + "/select", data, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
            ElNotification({
              type: "success",
              message: res.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //下发指令
    orderData(data: anyObj) {
      return new Promise((resolve, reject) => {
        _get(DeviceMLongRange + "/replace", data, "json", "")
          .then((res: any) => {
            console.log("下发指令", res);
            resolve(res);
            // ElNotification({
            //   type: "success",
            //   message: res.msg,
            // });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //查看指令数据
    dataDetail(code: any) {
      return new Promise((resolve, reject) => {
        _get("/system/command/getConfig/" + code, {}, "json", "")
          .then((res: any) => {
            console.log("查看指令数据", res);
            // this.overviewData = await getOverViewData(res.rows, [
            //   "lowtrange",
            //   "uptrange",
            //   "lowprange",
            //   "upprange",
            //   "lowfrange",
            //   "upfrange",
            //   "temperature",
            //   "pa",
            //   "flow",
            //   "current",
            //   "voltage",
            //   "hz",
            //   "model",
            // ]);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
  },
});
